//
//  XYTimePickerView.m
//  项目二时间选择器
//
//  Created by rimi on 15/12/17.
//  Copyright © 2015年 xiaoyi. All rights reserved.
//

#import "XYTimePickerView.h"
#import "CircularPickerView.h"
@interface XYTimePickerView ()
@property (nonatomic, strong) CircularPickerView *hourChoose;
@property (nonatomic, strong) CircularPickerView *minuteChoose;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *hourLabel;
@property (nonatomic, strong) UILabel *minuteLabel;
@property (nonatomic, strong) UILabel *maohaoLabel;

@property (nonatomic, strong) UIButton *backButton;
@end

@implementation XYTimePickerView

- (instancetype)initWithFrame:(CGRect)frame date:(NSString *)date {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0.992 green:0.957 blue:0.824 alpha:1.000];
        self.sendTime = [NSString stringWithString:date];

        if ([self isToday:self.sendTime]) {
            self.hour = [NSString stringWithFormat:@"%ld", [self getTheDefaultValue:@"HH"]];
            self.minute = [NSString stringWithFormat:@"%ld", [self getTheDefaultValue:@"mm"]];
        } else {
            self.hour = @"0";
            self.minute = @"0";
        }
        
        self.timeLabel.text = date;
        [self initUserInterface];
    }
    return self;
}

- (void)initUserInterface {
    [self addSubview:self.hourChoose];
    [self addSubview:self.minuteChoose];
    [self addSubview:self.timeLabel];
    
    [self addSubview:self.hourLabel];
    [self addSubview:self.maohaoLabel];
    [self addSubview:self.minuteLabel];
    [self addSubview:self.backButton];
    if (self.delegate) {
        [self.delegate isTheConfirmButtonEnabled];
    }
}

- (void)hourChoozeEvent:(CircularPickerView *)sender {
    if (sender.currentValue > 23.5) {
        _hour = [NSString stringWithFormat:@"0"];
        self.hourLabel.text = [NSString stringWithFormat:@"00"];
        sender.angle = 0;
        return;
    }
    
    if (sender.currentValue > 9.5) { //四舍五入后
        _hour = [NSString stringWithFormat:@"%.0lf", round(sender.currentValue)];
        self.hourLabel.text = [NSString stringWithFormat:@"%.0lf", round(sender.currentValue)];
    } else {
        _hour = [NSString stringWithFormat:@"%.0lf", round(sender.currentValue)];
        self.hourLabel.text = [NSString stringWithFormat:@"0%.0lf", round(sender.currentValue)];
    }
    
    [self activateTheConfirmButton];
}

- (void)minuteChoozeEvent:(CircularPickerView *)sender {
    if ((int)sender.currentValue % 5 != 0){
        return;
    }
    
    if ((int)sender.currentValue == 60) {
        _minute = [NSString stringWithFormat:@"0"];
        self.minuteLabel.text = [NSString stringWithFormat:@"00"];
        sender.angle = 0;
        return;
    }
    
    if (sender.currentValue > 9) {
        _minute = [NSString stringWithFormat:@"%ld", (NSInteger)(sender.currentValue)];
        self.minuteLabel.text = [NSString stringWithFormat:@"%ld", (NSInteger)(sender.currentValue)];
    } else {
        _minute = [NSString stringWithFormat:@"%ld", (NSInteger)(sender.currentValue)];
        self.minuteLabel.text = [NSString stringWithFormat:@"0%ld", (NSInteger)(sender.currentValue)];
    }
    [self activateTheConfirmButton];
}

#pragma mark - 自定义方法
- (CGFloat)timeViewWidth {
    return CGRectGetWidth(self.bounds);
}

- (CGFloat)timeViewHeight {
    return CGRectGetHeight(self.bounds);
}

- (NSDate *)getLocalDate:(NSDate *)date {
    NSTimeZone *localZone = [NSTimeZone localTimeZone];
    NSInteger interval = [localZone secondsFromGMTForDate:date];
    NSDate *mydate = [date dateByAddingTimeInterval:interval];
    return mydate;
}

- (NSString *)getDateFormatterDate:(NSString *)format date:(NSDate *)date{
    //选择的时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSString *string = [formatter stringFromDate:date];
    return string;
}

//获取滑轮初始值
- (NSInteger)getTheDefaultValue:(NSString *)DateFormat {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年 MMM dd日 EEEE"];
    NSDate *date = [formatter dateFromString:self.sendTime];
    
    if ([[self getDateFormatterDate:@"yyyyMMdd" date:[NSDate date]] isEqualToString:[self getDateFormatterDate:@"yyyyMMdd" date:date]]) {
        return [[self getDateFormatterDate:DateFormat date:[NSDate date]] integerValue];
    } else {
        return 0;
    }
}

//判断点入日期是否是今天
- (BOOL)isToday:(NSString *)dateString {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年 MMM dd日 EEEE"];
    NSDate *date = [formatter dateFromString:dateString];
    
    if ([[self getDateFormatterDate:@"yyyyMMdd" date:[NSDate date]] isEqualToString:[self getDateFormatterDate:@"yyyyMMdd" date:date]]) {
        return YES;
    } else {
        return NO;
    }
}

//判断是否让确定按钮激活
- (void)activateTheConfirmButton {
    if (![self isToday:self.sendTime]) {
        return;
    }
    
    if ( ([[self getDateFormatterDate:@"HH" date:[NSDate date]]integerValue] < [self.hour integerValue]) || (([[self getDateFormatterDate:@"HH" date:[NSDate date]] integerValue] <= [self.hour integerValue]) && ([[self getDateFormatterDate:@"mm" date:[NSDate date]] integerValue] < [self.minute integerValue]))) {
        if (self.delegate) {
            [self.delegate isTheConfirmButtonEnabled];
        }
    } else {
        if (self.delegate) {
            [self.delegate isTheConfirmButtonDisEnabled];
        }
    }
}

//返回日历选择页面
- (void)backTo {
    if (self.delegate) {
        [self.delegate backToCalenderPicker];
    }
}

#pragma mark - 懒加载
- (CircularPickerView *)hourChoose {
    if (!_hourChoose) {
        _hourChoose = [[CircularPickerView alloc] initWithFrame:CGRectMake(0, 150, 150, 150)];
        _hourChoose.backgroundColor = [UIColor colorWithRed:0.992 green:0.957 blue:0.824 alpha:1.000];
        
        _hourChoose.bgColor = [UIColor greenColor];
        _hourChoose.lineWidth = 25;
        _hourChoose.lineColor = [UIColor redColor];
        _hourChoose.minValue = 0;
        _hourChoose.maxValue = 24;
        _hourChoose.step = 0;
        _hourChoose.defaultValue = [self getTheDefaultValue:@"HH"];
        _hourChoose.autocomplete = YES;
        [_hourChoose addTarget:self action:@selector(hourChoozeEvent:) forControlEvents:UIControlEventValueChanged];
        [_hourChoose timePickerinit];
    }
    return _hourChoose;
}

- (CircularPickerView *)minuteChoose {
    if (!_minuteChoose) {
        _minuteChoose = [[CircularPickerView alloc] initWithFrame:CGRectMake(230, 150, 150, 150)];
        _minuteChoose.backgroundColor = [UIColor colorWithRed:0.992 green:0.957 blue:0.824 alpha:1.000];
        _minuteChoose.bgColor = [UIColor greenColor];
        _minuteChoose.lineWidth = 25;
        _minuteChoose.lineColor = [UIColor redColor];
        _minuteChoose.minValue = 0;
        _minuteChoose.maxValue = 60;
        _minuteChoose.step = 5;
        _minuteChoose.autocomplete = YES;
        _minuteChoose.defaultValue = [self getTheDefaultValue:@"mm"];
        
        [_minuteChoose addTarget:self action:@selector(minuteChoozeEvent:) forControlEvents:UIControlEventValueChanged];
        [_minuteChoose timePickerinit];
    }
    return _minuteChoose;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.center = CGPointMake([self timeViewWidth] / 2, 100);
        _timeLabel.bounds = CGRectMake(0, 0, 300, 40);
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        _timeLabel.font = [UIFont systemFontOfSize:25];
        //选择的时间
    }
    return _timeLabel;
}

- (UILabel *)hourLabel {
    if (!_hourLabel) {
        _hourLabel = [[UILabel alloc] init];
        _hourLabel.center = CGPointMake([self timeViewWidth] / 2 - 40, 60);
        _hourLabel.bounds = CGRectMake(0, 0, 100, 40);
        if ([self isToday:self.sendTime]) {
            _hourLabel.text = [self getDateFormatterDate:@"HH" date:[NSDate date]];
        } else {
            _hourLabel.text = @"00";
        }
        _hourLabel.textAlignment = NSTextAlignmentCenter;
        _hourLabel.font = [UIFont systemFontOfSize:25];
    }
    return _hourLabel;
}

- (UILabel *)maohaoLabel {
    if (!_maohaoLabel) {
        _maohaoLabel = [[UILabel alloc] init];
        _maohaoLabel.center = CGPointMake([self timeViewWidth] / 2 , 60);
        _maohaoLabel.bounds = CGRectMake(0, 0, 20, 40);
        _maohaoLabel.text = @":";
        _maohaoLabel.textAlignment = NSTextAlignmentCenter;
        _maohaoLabel.font = [UIFont systemFontOfSize:25];
    }
    return _maohaoLabel;
}

- (UILabel *)minuteLabel {
    if (!_minuteLabel) {
        _minuteLabel = [[UILabel alloc] init];
        _minuteLabel.center = CGPointMake([self timeViewWidth] / 2 + 40, 60);
        _minuteLabel.bounds = CGRectMake(0, 0, 100, 40);
        if ([self isToday:self.sendTime]) {
            _minuteLabel.text = [self getDateFormatterDate:@"mm" date:[NSDate date]];
        } else {
            _minuteLabel.text = @"00";
        }
        _minuteLabel.textAlignment = NSTextAlignmentCenter;
        _minuteLabel.font = [UIFont systemFontOfSize:25];
    }
    return _minuteLabel;
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _backButton.frame = CGRectMake(10, 10, 30, 30);
        [_backButton setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}
@end
